﻿using CacheManager.Core;
using K9Nano.Caching.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace K9Nano.Caching
{
    public static class CacheManagerStartupExtensions
    {
        public static IK9NanoAppBuilder AddTypeCacheManager(this IK9NanoAppBuilder builder, string redisConnection)
        {
            builder.Services.AddCacheManagerConfiguration(configure: builder =>
            {
                builder
                    .WithUpdateMode(CacheUpdateMode.Up)
                    .WithMicrosoftMemoryCacheHandle()
                    .And
                    .WithRedisConfiguration("redis", redisConnection)
                    .WithRedisBackplane("redis")
                    .WithRedisCacheHandle("redis", true);
                builder.WithJsonSerializer();
            });
            builder.Services.AddCacheManager();

            builder.Services.AddSingleton(typeof(ITypedCache<>), typeof(TypedCache<>));
            return builder;
        }
    }
}